//
//  RegisterViewController.m
//  News
//
//  Created by 宋浩文的pro on 16/5/22.
//  Copyright © 2016年 songhaowen. All rights reserved.
//

#import "RegisterViewController.h"
#import "CommonTool.h"

@interface RegisterViewController ()

@property (weak, nonatomic) IBOutlet UITextField *accountTF;

@property (weak, nonatomic) IBOutlet UITextField *passwordTF;

@property (weak, nonatomic) IBOutlet UITextField *rePasswordTF;

@property (weak, nonatomic) IBOutlet UIButton *registerBtn;

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _registerBtn.layer.cornerRadius = 5;
}

- (IBAction)back:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (IBAction)registerBtnClicked:(id)sender
{
    if (_passwordTF.text.length == 0 || _rePasswordTF.text.length == 0 || _accountTF.text.length == 0) {
        [CommonTool showError:@"请填写完整"];
        return;
    }
    
    if (_passwordTF.text != _rePasswordTF.text) {
        [CommonTool showError:@"两次密码不一致"];
        return;
    }
    
    BmobUser *bUser = [[BmobUser alloc] init];
    [bUser setUsername:_accountTF.text];
    [bUser setPassword:_passwordTF.text];
    [bUser signUpInBackgroundWithBlock:^ (BOOL isSuccessful, NSError *error){
        if (isSuccessful){
            [CommonTool showHUDWillDismissText:@"注册成功" withTime:1];
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            if (error.code == 202) {
                [CommonTool showError:@"此账号被占用"];
            } else {
                [CommonTool showError:@"网络错误"];
            }
        }
    }];
    
}

@end
